<template>
  <!-- 新增租户管理员 -->
  <div style="padding: 10px;">
    <Form :label-width="70" style="margin-top: -20px;margin-bottom:30px;">
      <Row>
        <Col span="12">
          <Form-item :label="$t('module.tenantManage.tenantCode')">
            <Input v-model="tenantInfo.tenantCode" disabled />
          </Form-item>
        </Col>
        <Col span="12">
          <Form-item :label="$t('module.tenantManage.tenantName')">
            <Input v-model="tenantInfo.tenantName" disabled />
          </Form-item>
        </Col>
      </Row>
    </Form>
    <InnerPageTable ref="tenantTable" :config="tableConfig" style="height: 492px;">
      <div slot="table-bar" style="margin-top: -20px; margin-bottom: 20px;">
        <RPButton optType="create" @click="add()" v-if="tenantInfo.hasDefaultUserCenter" />
        <RPButton optType="create" :text="$t('module.tenantManage.assginUser')" @click="assignUser()" />
        <RPButton optType="create" @click="createLdap" :text="$t('module.userManage.ldapcreate')" />
        <RPButton optType="batch" @click="deleteAdmin()" />
      </div>
    </InnerPageTable>
  </div>
</template>
<script>
import { queryAssignedUser, deleteAssignedUser, deleteAssignedUser2 } from '@/api/teantManageAPI.js'
import { userColumns } from './columns.js'
import locales from './locales.js'
import { RIPPLES_APP } from '@/const.js'
import TenantAdminEdit from './TenantAdminEdit.vue'
import assignUser from './assignUser.vue'
import UnlockManage from '@/views/userCenter/userManage/UnlockManage.vue'
import LdapManage from '@/views/userCenter/userManage/LdapManage.vue'
import userManageAPI from '@/api/userManageApi'

export default {
  name: 'addTenantAdmin',
  mixins: [locales],
  props: {
    tenantInfo: {
      type: Object,
      default: () => ({})
    },
    rowList: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      searchParams: {
        tenantCode: this.tenantInfo.tenantCode,
        excludeAuthorizedUsersByTenant: false
      },
      tableSelection: [],
      selectionUserId: [],
      administrator: 'administrator'
    }
  },
  computed: {
    tableConfig() {
      return {
        loadDataFn: this.getAssignUser,
        columnsFn: userColumns.bind(this),
        onSelectionChange: this.selection
      }
    },
    actions() {
      return [
        // {
        //   text: this.$t("common.close"),
        //   theme: "primary",
        //   handle: () => {
        //     this.$emit("Close");
        //   }
        // }
      ]
    }
  },
  created: function() {
    this.$emit('SetTitle', this.$t('module.tenantManage.administrator'))
    this.$emit('SetPageWidth', 734)
    this.$emit('SetPageActions', this.actions)
  },
  mounted() {
    this.search()
  },
  methods: {
    // 新建LADP账号
    createLdap() {
      new this.$pageModal(
        LdapManage,
        {
          props: {
            propsTenantCode: this.tenantInfo.tenantCode
          }
        },
        data => {
          if (data) this.search()
        }
      )
    },
    selection(tableVal) {
      this.tableSelection = []
      this.selectionUserId = []
      tableVal.forEach(item => {
        this.tableSelection.push(item.id)
        this.selectionUserId.push(item.userId)
      })
    },
    assignUser() {
      new this.$pageModal(
        assignUser,
        {
          props: { tenantCode: this.tenantInfo.tenantCode }
        },
        data => {
          if (data) this.search()
        }
      )
    },
    deleteAdmin() {
      if (this.tableSelection.length == 0) {
        this.$message.error({
          content: this.$t('common.warning.selctElement'),
          duration: 3
        })
        return
      }
      new $confirm(this.$t('common.warning.confirmToDelete'), this).then(() => {
        let param = {
          roleCode: 'admin-tenant',
          tenantCode: this.tenantInfo.tenantCode,
          userIds: this.tableSelection
        }
        //没有默认用户中心时header加orgTenantCode
        if (this.tenantInfo.hasDefaultUserCenter) {
          deleteAssignedUser(param)
            .then(() => {
              this.successFn()
              this.tableSelection = []
              // let alreadyData = {
              //   tenantCode: this.tenantInfo.tenantCode,
              //   userIds: this.selectionUserId
              // };
              // userManageAPI.alreadydelUser(alreadyData);
            })
            .catch(() => {})
        } else {
          deleteAssignedUser2(param)
            .then(() => this.successFn())
            .catch(() => {})
        }
      })
    },
    successFn() {
      this.$message.success({
        content: this.$t('common.deleteSuccess'),
        duration: 3
      })
      this.search()
    },
    add() {
      new this.$pageModal(
        TenantAdminEdit,
        {
          props: { tenantCode: this.tenantInfo.tenantCode }
        },
        data => {
          this.search()
        }
      )
    },
    search(page = 1) {
      // this.$refs["tenantTable"].load(Object.assign({}, this.formSearch), page);
      // let searchParams = Object.assign({}, this.formSearch);
      // if (searchParams.enabled == "ALL") {
      //   searchParams.enabled = null;
      // }
      // if (searchParams.applicationCode == "ALL") {
      //   searchParams.applicationCode = null;
      // }
      this.$refs['tenantTable'].load(this.searchParams, page)
    },
    getAssignUser({ responseProcess, param }) {
      queryAssignedUser(param).then(data => {
        responseProcess(data.data)
      })
    },
    unlock(row) {
      new this.$pageModal(
        UnlockManage,
        {
          props: {
            userId: row.id
          }
        },
        data => {
          this.$refs['tenantTable'].load(this.searchParams, 1)
        }
      )
    }
  },
  watch: {},
  directives: {}
}
</script>

<style lang="less"></style>
